package us.icitap.commandcenter.business;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;


import com.vaadin.server.StreamResource.StreamSource;

import us.icitap.commandcenter.business.ejb.persistence.Call;
import us.icitap.commandcenter.utils.Utils;
public class ExcelCallDocument implements StreamSource{

	private List<Call> callList;

	public ExcelCallDocument(List<Call> callList) {
		this.callList = callList;
		
	}
	
	public ByteArrayInputStream createDocument(List<Call> callList) throws IOException{
		
		Workbook wb;
		Sheet sheet;
		Row row;
		Cell cell;
		
		wb = new HSSFWorkbook();
		sheet = wb.createSheet("KOMUNIKATA DITORE");
		sheet.createRow(0);
		sheet.createRow(1);
		sheet.createRow(2);
		
		CellStyle cs = wb.createCellStyle();
		cs.setWrapText(true);
		cs.setAlignment(CellStyle.ALIGN_CENTER);
		cs.setFillBackgroundColor(IndexedColors.GREY_40_PERCENT.getIndex());
		cs.setFillPattern(CellStyle.LEAST_DOTS);
		Font f = wb.createFont();
		f.setBoldweight(Font.BOLDWEIGHT_BOLD);
		cs.setFont(f);
		cs.setBorderBottom(CellStyle.BORDER_THIN);
		
		row = sheet.createRow(3);
		
		cell = row.createCell(0);
		cell.setCellValue("Koha e telefonates");
		cell.setCellStyle(cs);
		
		cell = row.createCell(1);
		cell.setCellValue("Telefonuesi");
		cell.setCellStyle(cs);
		
		cell = row.createCell(2);
		cell.setCellValue("Arsyeja e telefonates");
		cell.setCellStyle(cs);
		
		cell = row.createCell(3);
		cell.setCellValue("Klasifikimi i telefonates");
		cell.setCellStyle(cs);
		
		cell = row.createCell(4);
		cell.setCellValue("Zona e mbulimit");
		cell.setCellStyle(cs);
		
		cell = row.createCell(5);
		cell.setCellValue("Pershkrimi");
		cell.setCellStyle(cs);
		
		cell = row.createCell(6);
		cell.setCellValue("Adresa");
		cell.setCellStyle(cs);
		
		cell = row.createCell(7);
		cell.setCellValue("Kush eshte njoftuar");
		cell.setCellStyle(cs);
		
		cell = row.createCell(8);
		cell.setCellValue("Patrullat e derguara");
		cell.setCellStyle(cs);
		
		cell = row.createCell(9);
		cell.setCellValue("Koha e verifikimit");
		cell.setCellStyle(cs);
		
		cell = row.createCell(10);
		cell.setCellValue("Arsyeja e tel pas verifikimit");
		cell.setCellStyle(cs);
		
		cell = row.createCell(11);
		cell.setCellValue("Shenime mbi verifikimin");
		cell.setCellStyle(cs);
		
		cell = row.createCell(12);
		cell.setCellValue("Masat e marra");
		cell.setCellStyle(cs);
		
		int i = 4;
		
		CellStyle style = wb.createCellStyle();
		
		for (Call call : callList) {
			row = sheet.createRow(i);
			cell = row.createCell(0);
			cell.setCellValue(Utils.formatedDateTime(call.getRecordDate()));
			cell = row.createCell(1);
			cell.setCellValue(call.getCaller());
			cell = row.createCell(2);
			cell.setCellValue(call.getCallReason()!=null?call.getCallReason().getTag() : "");
			cell = row.createCell(3);
			cell.setCellValue(call.getActualGrade()!=null?call.getActualGrade().getTag() : "");
			cell = row.createCell(4);
			cell.setCellValue(call.getMainDispatcher()!=null?call.getMainDispatcher().getName() : "");
			
			cell = row.createCell(5);
			cell.setCellValue(call.getDescription());
			cell = row.createCell(6);
			cell.setCellValue(call.getAddress());
			
			cell = row.createCell(7);
			cell.setCellValue(call.getNotificationsInfo());
			
			cell = row.createCell(8);
			cell.setCellValue(call.getPatrolsInfo());
			
			cell = row.createCell(9);
			cell.setCellValue(Utils.formatedDateTime(call.getVerificationDate()));
			
			cell = row.createCell(10);
			cell.setCellValue(call.getVerifiedCallReason()!=null ? call.getVerifiedCallReason().getTag() : "");
			
			cell = row.createCell(11);
			cell.setCellValue(call.getVerifNotesFormatted());
			
			cell = row.createCell(12);
			cell.setCellValue(call.getPoliceActionListString());
			i+=1;
		}
		
		for(int j = 0; j < 12; j++) {
			sheet.autoSizeColumn(j);
		}
		
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		wb.write(outputStream);
		
		return new ByteArrayInputStream(outputStream.toByteArray());
		

	}

	@Override
	public InputStream getStream() {
		// TODO Auto-generated method stub
		try {
			return this.createDocument(callList);
		} catch (Throwable e) {
			// TODO Auto-generated catch block
			throw new RuntimeException(e);
		}
	}
	

	
	
}
